package com.ruoyi.web.controller.snmp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SnmpSensorinfo;
import com.ruoyi.system.domain.SnmpSensubjectdevice;
import com.ruoyi.system.domain.SnmpSystem;
import com.ruoyi.system.service.ISnmpSensorinfoService;
import com.ruoyi.system.service.ISnmpSensubjectdeviceService;
import com.ruoyi.system.service.ISnmpSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@RestController
@RequestMapping("/snmp/system")
public class SnmpSystemController extends BaseController
{
    @Autowired
    private ISnmpSystemService snmpSystemService;
    @Autowired
    private ISnmpSensubjectdeviceService snmpSensubjectdeviceService;
    @Autowired
    private ISnmpSensorinfoService snmpSensorinfoService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:system:list')")
    @GetMapping("/list")
    public TableDataInfo list(SnmpSystem snmpSystem) {
        startPage();
        //根据配置的设备展示主列表
        List<SnmpSystem> list = snmpSystemService.selectSnmpSystemList(snmpSystem);

        for (SnmpSystem entity : list
        ) {
            SnmpSensubjectdevice obj = new SnmpSensubjectdevice();
            if (entity.getDeviceName() != null && entity.getDeviceName().length() > 0) {
                obj.setDeviceName(entity.getDeviceName());
            }
            if (entity.getDeviceId() != null && entity.getDeviceId().length() > 0) {
                obj.setDeviceId(entity.getDeviceId());
            }
            if (entity.getDeviceIp() != null && entity.getDeviceIp().length() > 0) {
                obj.setDeviceIp(entity.getDeviceIp());
            }
            List<SnmpSensubjectdevice> listSensubjectdevice = snmpSensubjectdeviceService.selectSnmpSensubjectdeviceList(obj);
            if (listSensubjectdevice.size() > 0) {
                entity.setSensorsubject(listSensubjectdevice.size() + "个传感器场景");
            } else {
                if (entity.getDeviceId() != null && entity.getDeviceId().length() > 0) {

                    SnmpSensorinfo snmpSensorinfo = new SnmpSensorinfo();
                    snmpSensorinfo.setDeviceId(entity.getDeviceId());
                    List<SnmpSensorinfo> Sensorlist = snmpSensorinfoService.selectSnmpSensorinfoList(snmpSensorinfo);
                    if (Sensorlist.size() > 0) {
                        entity.setSensorsubject(Sensorlist.size() + "个传感器场景");
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:system:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SnmpSystem snmpSystem)
    {
        List<SnmpSystem> list = snmpSystemService.selectSnmpSystemList(snmpSystem);
        ExcelUtil<SnmpSystem> util = new ExcelUtil<SnmpSystem>(SnmpSystem.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('snmp:system:query')")
    @GetMapping(value = "/{ip}")
    public AjaxResult getInfo(@PathVariable("ip") String ip) throws Exception {
        //SnmpSystem snmpSystem= snmpSystemService.selectSnmpSystemById(id);
        snmpSystemService.getBase(ip);//gaohong,每跑一次更新一次数据

        return success(snmpSystemService.getBaseInfo(ip));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('snmp:system:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SnmpSystem snmpSystem)
    {
        return toAjax(snmpSystemService.insertSnmpSystem(snmpSystem));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('snmp:system:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SnmpSystem snmpSystem)
    {
        return toAjax(snmpSystemService.updateSnmpSystem(snmpSystem));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('snmp:system:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(snmpSystemService.deleteSnmpSystemByIds(ids));
    }
}
